<?php
include_once 'form.class.php';

class field
{
    public $field_type = '';
    public $field_data;
    public $error;
    public $response;
    protected $db;
    protected $fieldname = '';

    public function __construct($field='')
    {
        global $response;
        $this->field_type = 'varchar';
        $this->response = &$response;
        if(!empty($field))
        {
            $this->field_data = &$field;
        }
    }

    /**
     * 检查设置项数据
     * @author menghao
     * @param <array> $data
     */
    function check_settings($data)
    {
        return true;
    }

    function check_data($data)
    {
        $field = $this->field_data;
        if(!$field['isempty'] && empty($data))
        {
            $this->error = "{$field['name']}不能为空！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        elseif(!empty($field['regex']) && !preg_match($field['regex'], $data))
        {
            $this->error = "{$field['name']}格式不正确！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        elseif($this->field_type == 'varchar' && strlen($data)>$this->field_data['length'])
        {
            $this->error = "{$field['name']}长度超出所允许的最大值{$this->field_data['length']}字节！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        return true;
    }

    function create_form($value='')
    {
        return '';
    }

    function creat_tip()
    {
        return $this->field_data['tips'];
    }

    function settings()
    {
        return false;
    }

    /**
     * 用户输入格式转换为存储格式
     * @param <mixed> $data
     * @return <string>
     */
    function get_data($data)
    {
        return $data;
    }

    /**
     * 
     */
    function fieldname()
    {
        return $this->fieldname;
    }
}
?>
